package com.morehair.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.admin.GroupOnRulesBo;
import com.morehair.bean.pojo.Goods;
import com.morehair.bean.pojo.GrouponRules;
import com.morehair.bean.pojo.GrouponRulesExample;
import com.morehair.bean.vo.admin.TotalAndItemsVo;
import com.morehair.mapper.GoodsMapper;
import com.morehair.mapper.GrouponRulesMapper;
import com.morehair.service.admin.GroupOnService;
import com.morehair.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Author  kkw
 * Date  2021/6/9 21:13
 * Version 1.0
 * Description :
 */
@Service
public class GroupOnServiceImpl implements GroupOnService {
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public TotalAndItemsVo<GrouponRules> getGroupOnRules(Integer page, Integer limit, Integer goodsId, String sort, String order) {
        PageHelper.startPage(page,limit);
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(sort+" "+order);
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
        long total = grouponRulesPageInfo.getTotal();
        TotalAndItemsVo<GrouponRules> totalAndItemsVo = new TotalAndItemsVo<>();
        totalAndItemsVo.setTotal(total);
        totalAndItemsVo.setItems(grouponRules);
        return totalAndItemsVo;
    }

    @Override
    public GrouponRules createGroupOnRules(GroupOnRulesBo groupOnRulesBo) {
        Integer goodsId = groupOnRulesBo.getGoodsId();
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        if (goods == null) {
            return null;
        }
        GrouponRules grouponRules = new GrouponRules();
        grouponRules.setGoodsId(goodsId);
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());
        grouponRules.setDiscount(groupOnRulesBo.getDiscount());
        grouponRules.setDiscountMember(groupOnRulesBo.getDiscountMember());
        grouponRules.setAddTime(new Date());
        grouponRules.setUpdateTime(new Date());
        grouponRules.setExpireTime(groupOnRulesBo.getExpireTime());
        grouponRules.setDeleted(false);
        grouponRulesMapper.insertSelective(grouponRules);
        return grouponRules;
    }

    @Override
    public void deleteGroupOnRules(GrouponRules grouponRules) {
        grouponRules.setDeleted(true);
        grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
    }

    @Override
    public boolean updateGroupOnRules(GrouponRules grouponRules) {
        Integer goodsId = grouponRules.getGoodsId();
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        if (goods == null) {
            return false;
        }
        grouponRules.setUpdateTime(new Date());
        grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
        return true;
    }
}
